Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
how to apply body.addClass("className") to iframe from the parent document

I have a WP page with embedded iframes. There is a widhet that changes the style of the page, and I need that it also changes the style of all embedded iframes. The style changes by clicking the button that has the following event handler:

NightVersionButton.click(function (event) {
    // this is a part that changes the page style (and it works)
    event.preventDefault();
    Cookies.set('contrast', 'night', { expires: 7, path: window.cookiePath });
    body.removeClass('highcontrast highcontrast2 highcontrast3');
    body.addClass('night');
    
    // this is a part that should change the style of all iframes, but it does not work due to the error "Uncaught TypeError: single_iframe.contentDocument.body.addClass is not a function"
    var all_iframes = document.getElementsByTagName('iframe');
    for (let single_iframe of all_iframes) {
        var iframe_doc = single_iframe.contentWindow ? single_iframe.contentWindow.document : single_iframe.contentDocument;
        iframe_doc.body.addClass('night');
        //$(single_iframe).contents().find('body').addClass('night'); // this jQuery alternative does not work neither
    }

});

However, the part of the JS code responsible for changing the styles in all iframes does not work, with the console error "Uncaught TypeError: single_iframe.contentDocument.body.addClass is not a function".

Please could you suggest the correct solution on how to change the JS that it will also apply addClass to all iframes?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!